package org.domeos.framework.api.controller.deployment;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.domeos.basemodel.HttpResponseTemp;
import org.domeos.basemodel.ResultStat;
import org.domeos.framework.api.consolemodel.deployment.VersionDraft;
import org.domeos.framework.api.controller.common.BaseSreController;
import org.domeos.framework.api.service.deployment.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sre/api/version")
@Api("SRE部署相关API")
public class VersionSreController extends BaseSreController {
	@Autowired
    VersionService versionService;
	
	
	@ApiOperation(value="创建部署版本")
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
	public HttpResponseTemp<?> createVersion(HttpServletRequest request,
			@RequestBody VersionDraft version,
			@RequestParam(value = "deployId") int deployId) throws Exception{
		String userId = getUserId(request);
		if (StringUtils.isEmpty(userId)) {
			return ResultStat.PARAM_ERROR.wrap("deployment userId is null!");
		}
		version.setUserId(getIntValue(userId));
		return versionService.sreCreateVersion(version, deployId);
		
	}
	
	@ApiOperation(value="查询部署版本")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public HttpResponseTemp<?> listVersion(@RequestParam(value="deployId",required=true) int deployId,
    		@RequestParam(value="tag",required=false) String tag) throws Exception {
        return versionService.sreListVersion(deployId,tag);
    }
	
	@ApiOperation(value="查询部署详细版本")
    @ResponseBody
    @RequestMapping(value = "/id/{deployId}/{versionId}", method = RequestMethod.GET)
    public HttpResponseTemp<?> getVersion(@PathVariable int deployId,
            @PathVariable int versionId) throws Exception {
        return versionService.sreGetVersion(deployId, versionId);
    }
	
	@ApiOperation(value="查询部署版本容器信息")
    @ResponseBody
    @RequestMapping(value = "/id/container", method = RequestMethod.GET)
    public HttpResponseTemp<?> getVersionContainerInfo(@RequestParam(value="deployId",required=true) int deployId,
    		@RequestParam(value="versionId",required =false,defaultValue="0") int versionId) throws Exception {
        return versionService.sreGetVersionContainerInfo(deployId, versionId);
    }

}
